package unittest;

import java.util.Map;

import org.junit.Test;

import treeedit.BBFAlgorithm;
import treeedit.BZAddressParser;
import treeedit.TedAlgorithm;
import treeedit.Tree;

public class AlgTest {
	String result = "fdacbe";

	@Test
	public void testBBFWithBolzanoAddressData() {

		Map<Integer, Tree> bzlAdd = BZAddressParser.parse("resources/L.trees");
		Map<Integer, Tree> bzrAdd = BZAddressParser.parse("resources/R.trees");

		System.out
				.println("Calculating distance between addresses that have the same ID.");
		for (int key : bzlAdd.keySet()) {
			Tree T1 = bzlAdd.get(key);
			Tree T2 = bzrAdd.get(key);
			T1.computeId(1);
			T2.computeId(1);

			int distance = -1;
			long currentTime = System.currentTimeMillis();
			// Will not calculate 2 trees when we know they have
			// distance > threshold / 5
			if (BBFAlgorithm.getDistance(T1, T2) <= BBFAlgorithm.threshold) {
				distance = TedAlgorithm.getDistance(T1, T2);
			}
			long runningTime = (System.currentTimeMillis() - currentTime);
			System.out.println(String.format("[%d] Distance = %s, Running time = %dms",
					key, distance == -1 ? "under treshold" : distance, runningTime));
		}
	}

	@Test
	public void testTedWithBolzanoAddressData() {
		Map<Integer, Tree> bzlAdd = BZAddressParser.parse("resources/L.trees");
		Map<Integer, Tree> bzrAdd = BZAddressParser.parse("resources/R.trees");

		System.out
				.println("Calculating distance between addresses that have the same ID.");
		for (int key : bzlAdd.keySet()) {
			Tree T1 = bzlAdd.get(key);
			Tree T2 = bzrAdd.get(key);
			T1.init();
			T2.init();

			TedAlgorithm ted = new TedAlgorithm();
			long currentTime = System.currentTimeMillis();
			int distance = ted.getDistance(T1, T2);
			long runningTime = (System.currentTimeMillis() - currentTime);

			System.out.println(String.format("[%d] Distance = %d, Running time = %dms",
					key, distance, runningTime));
		}
	}

}
